﻿using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using GSSoftSL.ViewModel.HocVienVM;
using GSSoftSL.Web;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace GSSoftSL.Views.HocVienView
{
    /// <summary>
    /// Description for HocVienMainView.
    /// </summary>
    public partial class HocVienMainView : UserControl
    {
        private EnumContentState _state = EnumContentState.Welcome;

        private HocVienSearchView _frmSearch;
        private HocVienMainViewModel _mainVM;

        /// <summary>
        /// Initializes a new instance of the HocVienMainView class.
        /// </summary>
        public HocVienMainView()
        {
            InitializeComponent();
            //GSSoftContext context = new GSSoftContext();
            //HocVienMainViewModel vm = new HocVienMainViewModel(context);
            _mainVM = this.DataContext as HocVienMainViewModel;
            //hvEdit.DataContext = _mainVM.HocVienEditViewModel;
            _frmSearch = new HocVienSearchView();//_mainVM.DC);
            Messenger.Default.Register<EnumContentState>(this, SetStatus);
        }
        
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            //((HocVienEditViewModel) hvEdit.DataContext).HocVien = new HocVien() {MaHV = ""};
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            _frmSearch.Show();
            _frmSearch.Closed += (s, arg) =>
                                     {
                                         //_mainVM.HocVien = _mainVM.HocVienEditViewModel.HocVien = _frmSearch.SelectedHocVien;
                                         _mainVM.HocVien = _frmSearch.SelectedHocVien;
                                         switch (_state)
                                         {
                                             case EnumContentState.LopHocVien:
                                                 _mainVM.HocVienKhoaHocViewModel.HocVien = _frmSearch.SelectedHocVien;
                                                 break;
                                             case EnumContentState.HocPhi:
                                                 _mainVM.HocVienHocPhiViewModel.HocVien = _frmSearch.SelectedHocVien;
                                                 break;
                                         }
                                         _mainVM.SetStatus(EnumAction.Detail);
                                     };
        }

        private void btnRegisterClass_Click(object sender, RoutedEventArgs e)
        {
            if (_mainVM.HocVien == null || _mainVM.HocVien.EntityState == EntityState.New || _mainVM.HocVien.EntityState == EntityState.Detached || _mainVM.IsEditable)
                return;
            _state = EnumContentState.LopHocVien;
            //HocVienMainViewModel vm = this.DataContext as HocVienMainViewModel;
            HocVienKhoaHocView view = new HocVienKhoaHocView();
            //view.DataContext = _mainVM.HocVienLopViewModel;
            _mainVM.HocVienKhoaHocViewModel.HocVien = null;
            pnlFunction.Content = view;
            _mainVM.HocVienKhoaHocViewModel.HocVien = _mainVM.HocVien;
            //vm.HocVien = ((HocVienMainViewModel)this.DataContext).HocVienEditViewModel.HocVien;
        }

        private void btnHocPhi_Click(object sender, RoutedEventArgs e)
        {
            if (_mainVM.HocVien == null || _mainVM.HocVien.EntityState == EntityState.New || _mainVM.HocVien.EntityState == EntityState.Detached || _mainVM.IsEditable)
                return;
            _state = EnumContentState.HocPhi;
            HocVienHocPhiView view = new HocVienHocPhiView();
            pnlFunction.Content = view;
            ((HocVienHocPhiViewModel)view.DataContext).HocVien = _mainVM.HocVien;
            ((HocVienHocPhiViewModel) view.DataContext).NewBienLaiCommand.Execute(null);
        }

        private void SetStatus(EnumContentState status)
        {
            switch (status)
            {
                case EnumContentState.Welcome:
                    break;
                case EnumContentState.HocPhi:
                    if (_mainVM.HocVien == null)
                        return;
                    _state = EnumContentState.HocPhi;
                    HocVienHocPhiView view = new HocVienHocPhiView();
                    pnlFunction.Content = view;
                    ((HocVienHocPhiViewModel) view.DataContext).HocVien = _mainVM.HocVien;
                    ((HocVienHocPhiViewModel) view.DataContext).NewBienLaiCommand.Execute(null);
                    break;
                case EnumContentState.LopHocVien:
                    if (_mainVM.HocVien == null)
                        return;
                    _state = EnumContentState.LopHocVien;
                    //HocVienMainViewModel vm = this.DataContext as HocVienMainViewModel;
                    HocVienKhoaHocView view1 = new HocVienKhoaHocView();
                    view1.DataContext = _mainVM.HocVienKhoaHocViewModel;
                    _mainVM.HocVienKhoaHocViewModel.HocVien = null;
                    pnlFunction.Content = view1;
                    _mainVM.HocVienKhoaHocViewModel.HocVien = _mainVM.HocVien;
                    break;
            }
        }

        public enum EnumContentState
        {
            Welcome,
            LopHocVien,
            HocPhi
        }
    }
}